package test;
import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail
{
    private static class SMTPAuthenticator extends Authenticator
    {

        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(username, password);
        }

        private String username;
        private String password;

        public SMTPAuthenticator(String username, String password)
        {
            this.username = null;
            this.password = null;
            this.username = username;
            this.password = password;
        }
    }


    private SendMail()
    {
    }

    public static void postMail(String recipients[], String subject, String message, String from, String hostname)
        throws MessagingException
    {
        postMail(recipients, subject, message, from, hostname, false, null, null);
    }

    public static void postMail(String recipients[], String subject, String message, String from, String hostname, boolean isAuth, String username, String password)
        throws MessagingException
    {
        Message msg = createMessage(recipients, subject, from, hostname, isAuth, username, password);
        msg.setContent(message, "text/html");
        Transport.send(msg);
    }

    public static void postMail(String recipients[], String subject, File fileName, String from, String hostname)
        throws MessagingException
    {
        postMail(recipients, subject, fileName, from, hostname, false, null, null);
    }

    public static void postMail(String recipients[], String subject, File fileName, String from, String hostname, boolean isAuth, String username, String password)
        throws MessagingException
    {
        Message msg = createMessage(recipients, subject, from, hostname, isAuth, username, password);
        FileDataSource fds = new FileDataSource(fileName);
        msg.setDataHandler(new DataHandler(fds));
        msg.setFileName(fds.getName());
        Transport.send(msg);
    }

    private static Message createMessage(String recipients[], String subject, String from, String hostname, boolean isAuthTrue, String username, String password)
        throws MessagingException
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", hostname);
        props.put("mail.smtp.auth", "false");
        Session session = null;
        if(isAuthTrue)
        {
            props.put("mail.smtp.auth", "true");
            Authenticator auth = new SMTPAuthenticator(username, password);
            session = Session.getDefaultInstance(props, auth);
        } else
        {
            session = Session.getDefaultInstance(props);
        }
        session.setDebug(false);
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        InternetAddress addressTo[] = new InternetAddress[recipients.length];
        for(int i = 0; i < recipients.length; i++)
            addressTo[i] = new InternetAddress(recipients[i]);

        msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
        msg.setSubject(subject);
        return msg;
    }

    public static void main(String args[])
        throws Exception
    {
/*        String rec[] = {
            "drengasa"
        };
        postMail(rec, "Test", "HI how are you", "svarutha@cisco.com", "xmb-bgl-412.cisco.com");*/
        String rec[] = {
                "dhanaren"
            };
            postMail(rec, "Test", "HI how are you", "dhanaren@gmail.com", "smtp.gmail.com");
    }

//    public static final String SMTP_HOST_NAME = "xmb-bgl-412.cisco.com";
    public static final String SMTP_HOST_NAME = "smtp.gmail.com";
}
